对称算法加密
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace NetOffice.Component
{
/// <summary>
/// Encryt 的摘要说明。
/// </summary>
public class Encryt
{
private SymmetricAlgorithm mCSP=new DESCryptoServiceProvider();
// public Encryt()
// {
//
// }
/// <summary>
/// 带参数的构造函数
/// </summary>
/// <param name="key">设置对称算法的机密密钥</param>
/// <param name="iv">设置对称算法的初始化向量</param>
public Encryt(string key,string iv)
{
mCSP.Key=Convert.FromBase64String(key);
mCSP.IV=Convert.FromBase64String(iv);
}
//加密函数
public string EncryStr(string Encryed)
{
ICryptoTransform ct; //定义基本的加密转换运算
MemoryStream ms; //创建其支持存储区为内存的流
CryptoStream cs; //定义将数据流链接到加密转换的流
byte[] byt;
//使用指定的密钥和初始化向量创建对称加密器对象
ct=mCSP.CreateEncryptor(mCSP.Key,mCSP.IV);
//将传来的值转换成字节数组
byt=Encoding.UTF8.GetBytes(Encryed);
ms=new MemoryStream();
cs=new CryptoStream(ms,ct,CryptoStreamMode.Write);
cs.Write(byt,0,byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
//解密函数
public string DenString(string value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
//创建对称斛密器对象
ct=mCSP.CreateDecryptor(mCSP.Key,mCSP.IV);
byt=Convert.FromBase64String(value);
ms=new MemoryStream ();
cs=new CryptoStream(ms,ct,CryptoStreamMode.Write);
cs.Write(byt,0,byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
<appSettings>
<add key="ConnectionString" value="server=.;uid=sa;database=NetOffice"/>
<add key="myKey" value="tIL+B4Fj3G4="/>
<add key="myIv" value="+Hz8mzPgE5c="/>
</appSettings>
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace NetOffice.Component
{
/// <summary>
/// Encryt 的摘要说明。
/// </summary>
public class Encryt
{
private SymmetricAlgorithm mCSP=new DESCryptoServiceProvider();
// public Encryt()
// {
//
// }
/// <summary>
/// 带参数的构造函数
/// </summary>
/// <param name="key">设置对称算法的机密密钥</param>
/// <param name="iv">设置对称算法的初始化向量</param>
public Encryt(string key,string iv)
{
mCSP.Key=Convert.FromBase64String(key);
mCSP.IV=Convert.FromBase64String(iv);
}
//加密函数
public string EncryStr(string Encryed)
{
ICryptoTransform ct; //定义基本的加密转换运算
MemoryStream ms; //创建其支持存储区为内存的流
CryptoStream cs; //定义将数据流链接到加密转换的流
byte[] byt;
//使用指定的密钥和初始化向量创建对称加密器对象
ct=mCSP.CreateEncryptor(mCSP.Key,mCSP.IV);
//将传来的值转换成字节数组
byt=Encoding.UTF8.GetBytes(Encryed);
ms=new MemoryStream();
cs=new CryptoStream(ms,ct,CryptoStreamMode.Write);
cs.Write(byt,0,byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
//解密函数
public string DenString(string value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
//创建对称斛密器对象
ct=mCSP.CreateDecryptor(mCSP.Key,mCSP.IV);
byt=Convert.FromBase64String(value);
ms=new MemoryStream ();
cs=new CryptoStream(ms,ct,CryptoStreamMode.Write);
cs.Write(byt,0,byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
<appSettings>
<add key="ConnectionString" value="server=.;uid=sa;database=NetOffice"/>
<add key="myKey" value="tIL+B4Fj3G4="/>
<add key="myIv" value="+Hz8mzPgE5c="/>
</appSettings>